﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Xml;

namespace Logs
{
    /// <summary>
    /// Logs 的摘要说明。
    /// </summary>
    public class WebLogs
    {
        private string logPath = System.Configuration.ConfigurationManager.AppSettings["errorlogpath"] + "";
        public WebLogs()
        {
        }
        public WebLogs(string LogPath)
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            logPath = LogPath;
        }

        public string FloderPath
        {
            get
            {
                return logPath;
            }
            set
            {
                logPath = value;
            }
        }

        public void Add(string LogType, string UserName, string message, string Content, string IP, string Agent, string Url, string UrlReferrer)
        {
            string filename = LogType + "-" + DateTime.Now.ToShortDateString() + ".xml";
            if (!System.IO.Directory.Exists(System.Configuration.ConfigurationManager.AppSettings["errorlogpath"] + ""))
            {
                System.IO.Directory.CreateDirectory(System.Configuration.ConfigurationManager.AppSettings["errorlogpath"] + "");
            }
            string filepath = logPath + filename;
            LogWrite(filepath, UserName, message, Content, IP, Agent, Url, UrlReferrer);
            //插入错误日志
            string sqlinsertlog = "insert into weblog(message,Contents,IP,Agent,Url,UrlReferrer) values('" + functions.chgSql(message) + "','" + functions.chgSql(Content) + "','" + functions.chgSql(IP) + "','" + functions.chgSql(Agent) + "','" + functions.chgSql(Url) + "','" + functions.chgSql(UrlReferrer) + "')";

            SqlHelper.ExecuteSQL(sqlinsertlog);
            if (IP != "127.0.0.1")
            {
                //插入错误日志
                
                SendLogMail(filepath, UserName, message, Content, IP, Agent, Url, UrlReferrer);
            }
        }

        private void SendLogMail(string filepath, string UserName, string message, string Content, string IP, string Agent, string Url, string UrlReferrer)
        {
            string strContent = "<font color=\"red\">AMD_XLHD网站中发生如下错误：</font><br><br><font color=\"#E66500\">FilePath:</font>" + filepath
                                + "<br/><br/><font color=\"#E66500\">UserName:</font>" + UserName
                                + "<br/><br/><font color=\"#E66500\">Messages:</font>" + message
                                + "<br/><br/><font color=\"#E66500\">Content:</font>" + Content
                                + "<br/><br/><font color=\"#E66500\">IP:</font>" + IP
                                + "<br/><br/><font color=\"#E66500\">Agent:</font>" + Agent
                                + "<br/><br/><font color=\"#E66500\">Url:</font>" + Url
                                + "<br/><br/><font color=\"#E66500\">UrlReferrer:</font>" + UrlReferrer;

            strContent = "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
                                + "<head>"
                                + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />"
                                + "<title></title>"
                                + "<style type=\"text/css\">"
                                + ".bt {"
                                + "	font-family: \"Arial\", \"Helvetica\", \"sans-serif\";"
                                + "	font-size: 9pt;"
                                + "	line-height: normal;"
                                + "	color: #000000;"
                                + "</style>"
                                + "</head>"
                                + "<body >"
                                + "<div class=\"bt\">"
                                + strContent
                                + "<br /><br/>"
                                + "</div>"
                                + "</body>"
                                + "</html>";

            functions.SendEmail2("cherry.zhao@micromarketing.com.cn;xueying.li@micromarketing.com.cn;ethan.wang@micromarketing.com.cn;guofei.chang@micromarketing.com.cn", "", "", "AMD_XLHDWebsite", "engineer", "AMD_XLHDWebsite", strContent, "ERROR Message", "text/html", "");
        }

        private void LogWrite(string filepath, string UserName, string message, string Content, string IP, string Agent, string Url, string UrlReferrer)
        {
            try
            {
                XmlDocument xdoc = new XmlDocument();
                if (!File.Exists(filepath))
                {
                    xdoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><?xml-stylesheet type=\"text/xsl\" href=\"report.xsl\"?>" +
                                 "<WebLogs></WebLogs>");

                }
                else
                {

                    xdoc.Load(filepath);

                }

                // 创建一个新的日志节点并将它添加到根节点下
                XmlElement parentNode = xdoc.CreateElement("WebLog");
                xdoc.DocumentElement.PrependChild(parentNode);
                // 创建所有用于存储信息的节点
                XmlElement nameNode = xdoc.CreateElement("UserName");
                XmlElement messageNode = xdoc.CreateElement("Message");
                XmlElement contentNode = xdoc.CreateElement("Content");
                XmlElement timeNode = xdoc.CreateElement("Time");
                XmlElement ipNode = xdoc.CreateElement("IP");
                XmlElement agentNode = xdoc.CreateElement("Agent");
                XmlElement urlNode = xdoc.CreateElement("Url");
                XmlElement referrerNode = xdoc.CreateElement("Referrer");
                // 获取文本信息
                XmlText nameText = xdoc.CreateTextNode(UserName);
                XmlText messageText = xdoc.CreateTextNode(message);
                XmlText contentText = xdoc.CreateTextNode(Content);
                XmlText timeText = xdoc.CreateTextNode(DateTime.Now.ToString());
                XmlText ipText = xdoc.CreateTextNode(IP);
                XmlText agentText = xdoc.CreateTextNode(Agent);
                XmlText urlText = xdoc.CreateTextNode(Url);
                XmlText referrerText = xdoc.CreateTextNode(UrlReferrer);
                // 将上面创建的各个存储信息的节点添加到guest节点下但并不包含最终的值
                parentNode.AppendChild(nameNode);
                parentNode.AppendChild(messageNode);
                parentNode.AppendChild(contentNode);
                parentNode.AppendChild(timeNode);
                parentNode.AppendChild(ipNode);
                parentNode.AppendChild(agentNode);
                parentNode.AppendChild(urlNode);
                parentNode.AppendChild(referrerNode);
                // 将上面获取的文本信息添加到与之相对应的节点中
                nameNode.AppendChild(nameText);
                messageNode.AppendChild(messageText);
                contentNode.AppendChild(contentText);
                timeNode.AppendChild(timeText);
                ipNode.AppendChild(ipText);
                agentNode.AppendChild(agentText);
                urlNode.AppendChild(urlText);
                referrerNode.AppendChild(referrerText);

                // 保存存储信息的XML文件

                xdoc.Save(filepath);

                xdoc = null;
            }
            catch
            { }

        }
    }
}
